<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import=" org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.nio.file.Files" %>
<%@ page import="java.nio.file.StandardCopyOption" %>
<%--
  Created by IntelliJ IDEA.
  User: Peter Zhou
 
  To change this template use File | Settings | File Templates.
--%>


<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //项目根路径
    String path1 = request.getContextPath();
//获得url根路径
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path1;

//获得项目相对路径的真实路径 D:\学习\服装购买系统\ShoppingClothes\out\artifacts\ShoppingClothes_Web_exploded\img
    String path2 = application.getRealPath("img");


    //判断请求信息是否是文件上传表单提交的请求

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // 设置请求的编码格式为UTF-8
        request.setCharacterEncoding("UTF-8");

        // 创建FileItemFactory对象
        FileItemFactory factory = new DiskFileItemFactory();
        // 创建ServletFileUpload
        ServletFileUpload fileUpload = new ServletFileUpload(factory);
        // 解析请求
        List<FileItem> fileItemList = fileUpload.parseRequest(request);

        for (FileItem fileItem : fileItemList) {
            // 判断是否是普通表单字段
            if (!fileItem.isFormField()) {
                out.println("这是文件");
                // 获取文件名，并转换编码
                String fileName = new String(fileItem.getName().getBytes("ISO-8859-1"), "UTF-8");
                String ext = fileName.substring(fileName.lastIndexOf('.') + 1);
                out.println("文件名：" + fileName);
                out.println("文件名后缀名：" + ext);
                //后缀规定
                String[] exts = {"png", "gif", "bmp", "jpg"};
                List<String> extList = Arrays.asList(exts);
                if (extList.contains(ext)) {
                    String webPath = application.getRealPath("img") + "/" + fileName;
                    File fileOnWeb = new File(webPath);
                    fileItem.write(fileOnWeb);

                    String localPath = "D:\\学习\\服装购买系统\\ShoppingClothes\\web\\img\\" + fileName;
                    File fileOnLocal = new File(localPath);
                    Files.copy(fileOnWeb.toPath(), fileOnLocal.toPath(), StandardCopyOption.REPLACE_EXISTING); // 使用Java NIO复制文件

                    out.println("文件名：" + fileName);
                    out.println("文件保存成功");
                } else {
                    out.print("文件上传失败:文件格式必须是\"png\",\"gif\",\"bmp\",\"jpg\"");
                }

            } else {
                String name = new String(fileItem.getFieldName().getBytes("ISO-8859-1"), "UTF-8");
                String value = fileItem.getString("UTF-8");
                out.println(name + "=" + value);
            }
        }
        out.println("这是文件上传表单请求提交");
    } else {
        out.println("不是");
        response.sendRedirect("testUpFile.jsp");
    }
%>
</body>
</html>
